<?php namespace App\Http\Controllers\Api;

use App\Exceptions\NotFoundException;
use App\Exceptions\SecurityException;
use App\Facilitator;
use App\Org;
use App\User;
use Illuminate\Auth\Guard;
use Illuminate\Support\Facades\Input;
use Response;

class OrgController extends ApiController
{
    public function setOrgInfo(Guard $guard){
        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var User $user */
        $user = $guard->user();
        $facInfo = Facilitator::whereUserId($user->id)->first();
        if(!$facInfo)
            throw new NotFoundException(NotFoundException::UserInfoNotFound);

        $orgInfo = Org::whereFacId($facInfo->id)->first();
        if(!$orgInfo){
            $orgInfo = new Org();
            $orgInfo->fac()->associate($facInfo);
        }

        $type         = Input::get('type');
        $peopleDesc   = Input::get('people_desc');
        $orgName      = Input::get('org_name');
        $orgDesc      = Input::get('org_desc');
        $province     = Input::get('o_province');
        $city         = Input::get('o_city');
        $area         = Input::get('o_county');
        $experience   = Input::get('experience');
        $hasInvoice   = Input::get('has_invoice');
        $teamNum      = Input::get('team_num');
        $solution     = Input::get('solution');
        $otherService = Input::get('other_service');
        $imgLogoId    = Input::get('logo_id');
        $state        = Input::get('state');

        if(!is_null($type))$orgInfo -> org_type              = $type;
        if(!is_null($peopleDesc))$orgInfo ->people_desc      = $peopleDesc;
        if(!is_null($orgName))$orgInfo -> org_name           = $orgName;
        if(!is_null($orgDesc))$orgInfo -> org_desc           = $orgDesc;
        if(!is_null($province))$orgInfo -> province          = $province;
        if(!is_null($city))$orgInfo -> city                  = $city;
        if(!is_null($area))$orgInfo -> area                  = $area;
        if(!is_null($experience))$orgInfo -> experience      = $experience;
        if(!is_null($hasInvoice))$orgInfo -> has_invoice     = $hasInvoice;
        if(!is_null($teamNum))$orgInfo -> team_num           = $teamNum;
        if(!is_null($solution))$orgInfo -> solution          = $solution;
        if(!is_null($otherService))$orgInfo -> other_service = $otherService;
        if(!is_null($imgLogoId))$orgInfo -> img_logo_id      = $imgLogoId;
        if(!is_null($state))$orgInfo -> state                = $state;

        $orgInfo->save();

        $field = !is_null(Input::get('field'))?Input::get('field'):1;
        if(!is_null($field)){
            $oldTags = $orgInfo->fields()->lists('field_id');
            if(!$oldTags){
                $orgInfo->fields()->attach($field);
            }else{
                $orgInfo->fields()->detach($oldTags);
                $orgInfo->fields()->attach($field);
            }
        }

        $skill = !is_null(Input::get('skill'))?Input::get('skill'):1;
        if(!is_null($skill)){
            $oldTags = $orgInfo->skills()->lists('skill_id');
            if(!$oldTags){
                $orgInfo->skills()->attach($skill);
            }else{
                $orgInfo->skills()->detach($oldTags);
                $orgInfo->skills()->attach($skill);
            }
        }

        return $this->buildResponse(trans('api.set_org_info.success'),$orgInfo);
    }

    public function getLogoByUserId($userId)
    {
        $user = User::find($userId);
        $orgInfo = Facilitator::whereUserId($userId)->first()->org;

        if(!$orgInfo)
            return Response::download('images/userlogo.jpg','userlogo.jpg',[],'inline');

        if(!$orgInfo->img_logo_id)
            return Response::download('images/userlogo.jpg','userlogo.jpg',[],'inline');

        if(!$user)
            throw new NotFoundException(NotFoundException::UserNotFound);

        $photo = $orgInfo->photo;

        if(!$photo || $photo == null)
            return Response::download('images/userlogo.jpg','userlogo.jpg',[],'inline');

        $photoFile = $photo->files;

        if(!$photoFile || $photoFile == null)
            return Response::download('images/userlogo.jpg','userlogo.jpg',[],'inline');

        return Response::download($photoFile->getLocalPath(),$photoFile->file_name,[],'inline');
    }
}